/*
语法百题 解题报告：
 http://git.oschina.net/et0662/sznoi-grammar/ 
*/
#include<stdio.h>

int main(void)
{
	int w,s,y,m,i,d=0,leap;
	int a[2][12]={{31,28,31,30,31,30,31,31,30,31,30,32},{31,29,31,30,31,30,31,31,30,31,30,32}};
	scanf("%d%d",&y,&m);
	s=y-1+((y-1)/4)-((y-1)/100)+((y-1)/400)+1;
	w=s % 7;
	
	leap= ((y%400 ==0) || ((y%4==0)&&(y%100!=0)))?1:0;
	for(i=0;i<m-1;i++)
		d=d+a[leap][i];
	/*printf("%d",(w+d)%7);*/
	printf("Sun Mon Tue Wed Thu Fri Sat \n");
	w=(w+d)%7;
	s=0;
	for(i=0;i<w;i++)
	{
		printf("    ");
		s++;
	}
	for(i=0;i<a[leap][m-1];i++)
	{
		s++;
		printf("%-4d",i+1);
		if(s==7)
		{
			printf("\n");
			s=0;
		}
	}
	return 0;
}
